********************************************************************************
** Do file: g12_compliance_investigation.do
** First started: May 20, 2023
** Last edited: September 8, 2023

/* Purpose: this do file looks into compliance
*/

********************************************************************************

clear
clear matrix
clear mata
set maxvar 10000

* Fact for paper: What % of loans were not in compliance?

* Load dataset
use "$repay_clean/fenix_repay_extend_07172020_rep.dta", clear

* Merge in information for sample
merge m:1 accountid using "$merged/key_rep.dta", keepusing(k_complete_may k_rolling_list k_interacted_success k_surveyed k_tookloan_repay)

* Develop tag for sample
g tag = 1 if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1

* Examine share in compliance for the sample and treatment groups of interest, by first day
tab complier_share_wupg if loandayselapsed==1 & tag==1 & (treatmenttype_sh=="R T1-L" | treatmenttype_sh=="R T1-U" | treatmenttype_sh=="R T2-U") // 16%


********

* Fact for paper: What % of loan days were out of compliance?
sum complier_share_wupg if loandayselapsed==200 & tag==1 & (treatmenttype_sh=="R T1-L" | treatmenttype_sh=="R T1-U" | treatmenttype_sh=="R T2-U")

* 94% of days were in compliance, so 6% were not in compliance


********

* Fact for paper: How many days were loans not in compliance on average?
* Keep observations at or before 200 days, for those tagged and those where compliance variable is zero
keep if loandayselapsed <= 200 & complier_share_wupg==0 & tag==1

* Generate counter variable
g counter = 1 

* Sum counter variable
collapse (sum) counter, by(loanid treatmenttype_sh)

* Examine counter variable
sum counter if (treatmenttype_sh=="R T1-L" | treatmenttype_sh=="R T1-U" | treatmenttype_sh=="R T2-U") & counter!=200 // 17 days


********

* Fact for paper: How many loans fall out of compliance?

* Load dataset
use "$repay_clean/fenix_repay_extend_07172020_rep.dta", clear

* Merge in information for sample
merge m:1 accountid using "$merged/key_rep.dta", keepusing(k_complete_may k_rolling_list k_interacted_success k_surveyed k_tookloan_repay)

* Develop tag for sample, on first day
g tag = 1 if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & loandayselapsed==1 & complier_share_wupg==1

* Apply tag to entire loan
bys loanid (loandayselapsed): egen maxtag = max(tag)

* Drop day zero
drop if loandayselapsed==0

* Examine number of loans (exclude Secured loan) that are then not in compliance
codebook loanid if maxtag==1 & (treatmenttype_sh=="R T1-U" | treatmenttype_sh=="R T2-U") & complier_share_wupg!=1 & loandayselapsed <= 200 // 20 loans

* Do these loans come back into compliance?
keep if loandayselapsed <= 200
g steps = complier_share_wupg - complier_share_wupg[_n-1]

codebook loanid if steps < 0 & maxtag==1 & (treatmenttype_sh=="R T1-U" | treatmenttype_sh=="R T2-U") & complier_share_wupg!=1 & loandayselapsed <= 200 // all 20 do not come back into compliance
